package dilmaj.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import dilmaj.client.AreaJDO;
import dilmaj.client.AreaService;
import dilmaj.client.TermJDO;
import dilmaj.client.TermService;
import dilmaj.shared.AreaVO;
import dilmaj.shared.TermVO;

public class AreaServiceImpl extends RemoteServiceServlet implements
		AreaService {

	public List<AreaVO> getAllAreas() {
		ArrayList<AreaVO> areaVOs=new ArrayList<AreaVO>();
		
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    String query = "select from " + AreaJDO.class.getName();
	    List<AreaJDO> AreaJDOs = (List<AreaJDO>) pm.newQuery(query).execute();
	    
	    Iterator iterator=AreaJDOs.iterator();
	    while (iterator.hasNext()) {
	    	AreaJDO currentArea=(AreaJDO)iterator.next();
	    	areaVOs.add(new AreaVO(currentArea));
	    }
	    
	    pm.close();
	    return areaVOs;
	}

	public AreaVO addArea(AreaVO newArea) {
        AreaJDO AreaJDO = new AreaJDO(newArea);

        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(AreaJDO);
        } finally {
            pm.close();
        }
        
        newArea.setId(AreaJDO.getId());
        
        return newArea;
	}

}
